package com.example.hyclipboard;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.google.gson.Gson;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class HttpClient {
	private static final String TAG = "HOYIN_HttpClient";
	private final String server;
	private final int port;
	private final Handler handler;

	public static final int CONN_FAILED = -1;
	public static final int CONN_SUCCESS = 1;

	private final OkHttpClient client = new OkHttpClient().newBuilder().build();
	private final Gson gson = new Gson();

	@SuppressLint("HandlerLeak")
	HttpClient(String server, int port, Handler handler) {
		this.server = server;
		this.port = port;
		this.handler = handler;
	}

	public void send(String data) {
		Map map = this.gson.fromJson(data, Map.class);
		String action = (String) map.get(Macros.DBTags.ACTION);

		new Thread(() -> {

			RequestBody body = RequestBody.create(data, MediaType.parse("application/json"));
			String api = String.format("http://%s:%s%s", this.server, this.port, APIs.getAPI(action));
			Request request = new Request.Builder()
					.url(api)
					.method("POST", body)
////					.addHeader("Content-Type", "application/json")
					.build();

			try {
				Response response = client.newCall(request).execute();
				Message msg = Message.obtain();
				msg.what = CONN_SUCCESS;
				msg.obj = Objects.requireNonNull(response.body()).string();
				handler.sendMessage(msg);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}).start();
	}

	private static class SignUpRequest {
		String action;
		String nickname;
		String account;
		String password;
	}

	public static class SignUpResponse {
		String status;
		String uid;
		String reason;
	}

	public static String generateSignUpRequestBody(String nickname, String account, String password) {
		Gson gson = new Gson();
		SignUpRequest request = new SignUpRequest();
		request.action = Macros.ACTIONS.SIGNUP;
		request.account = account;
		request.password = password;
		request.nickname = nickname;
		return gson.toJson(request);
	}

	private static class LoginRequest {
		String action;
		String account;
		String password;
	}

	public static class LoginResponse {
		String status;
		String uid;
		String nickname;
		String account;
		String reason;
	}


	public static String generateLoginRequestBody(String account, String password) {
		Gson gson = new Gson();
		LoginRequest request = new LoginRequest();
		request.action = Macros.ACTIONS.LOGIN;
		request.account = account;
		request.password = password;
		return gson.toJson(request);
	}

	private static class PushRequest {
		String action;
		String uid;
		String time;
		String device;
		String content;
	}

	public static class PushResponse {
		String status;
		String uid;
		String cid;
		String reason;
	}

	public static String generatePushRequestBody(String uid, String time, String content, String deviceName) {
		Gson gson = new Gson();
		PushRequest request = new PushRequest();
		request.action = Macros.ACTIONS.PUSH;
		request.uid = uid;
		request.time = time;
		request.content = content;
		request.device = deviceName;
		return gson.toJson(request);
	}

	private static class QueryRequest {
		String action;
		String uid;
		String device;
	}

	public static String generateQueryRequestBody(String uid, String deviceName) {
		Gson gson = new Gson();
		QueryRequest request = new QueryRequest();
		request.action = Macros.ACTIONS.QUERY;
		request.uid = uid;
		request.device = deviceName;
		return gson.toJson(request);
	}

	public static class QueryResponse {
		String action;
		String status;
		String reason;
		List<HttpClient.MemoData> items;
	}

	private static class FetchRequest {
		String action;
		String cid;
	}

	public static String generateFetchRequestBody(String cid) {
		Gson gson = new Gson();
		FetchRequest request = new FetchRequest();
		request.action = Macros.ACTIONS.FETCH;
		request.cid = cid;
		return gson.toJson(request);
	}

	public static class FetchResponse {
		String reason;
		String action;
		String status;
		String cid;
		String content;
		String time;
		Boolean read;
		String device;
	}

	private static class ReadRequest {
		String action;
		String cid;
	}

	public static String generateReadRequestBody(String cid) {
		Gson gson = new Gson();
		ReadRequest request = new ReadRequest();
		request.action = Macros.ACTIONS.READ;
		request.cid = cid;
		return gson.toJson(request);
	}

	public static class ReadResponse {
		String action;
		String reason;
		String status;
	}

	public static class MemoData {
		String cid;
		String time;
		String device;
		String content;
		Boolean read;
	}
}
